package com.dgshanger.sanhxiaofeisc.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.dgshanger.sanhxiaofeisc.MainActivity;
import com.dgshanger.sanhxiaofeisc.entity.PushExtraEntity;
import com.dgshanger.sanhxiaofeisc.ui.home.activity.PayResultActivity;
import com.dgshanger.sanhxiaofeisc.ui.person.MessageActivity;
import com.dgshanger.sanhxiaofeisc.ui.person.PayForDetailActivity;
import com.google.gson.Gson;

import cn.jpush.android.api.JPushInterface;

/**
 * NotificationReceiver
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/14 20:53.
 */

public class NotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String extras = intent.getStringExtra(JPushInterface.EXTRA_EXTRA);
        PushExtraEntity pushExtraEntity = new Gson().fromJson(extras, PushExtraEntity.class);

        //判断app进程是否存活
        //if (AppUtils.isAppForeground()) {
        //如果存活的话，就直接启动DetailActivity，但要考虑一种情况，就是app的进程虽然仍然在
        //但Task栈已经空了，比如用户点击Back键退出应用，但进程还没有被系统回收，如果直接启动
        //DetailActivity,再按Back键就不会返回MainActivity了。所以在启动
        //DetailActivity前，要先启动MainActivity。
        Intent mainIntent = new Intent(context, MainActivity.class);
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Intent mIntent = null;
        if (PushExtraEntity.CODE_NEW_MESSAGE.equals(pushExtraEntity.getPush_code())) {
            //新消息
            mIntent = new Intent(context, MessageActivity.class);
            mIntent.putExtra(Constants.KEY_INTENT_MSG_ID, pushExtraEntity.getRecord_id());
        } else if (PushExtraEntity.CODE_PAY_FOR.equals(pushExtraEntity.getPush_code())) {
            //收到代付申请
            mIntent = new Intent(context, PayForDetailActivity.class);
            mIntent.putExtra(Constants.KEY_INTENT_MSG_ID, pushExtraEntity.getRecord_id());
        } else if (PushExtraEntity.CODE_PAY_SUCCESS.equals(pushExtraEntity.getPush_code())) {
            //付款成功
            mIntent = new Intent(context, PayResultActivity.class);
            mIntent.putExtra("orderId", pushExtraEntity.getRecord_id());
        }
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Intent[] intents = {mainIntent, intent};
//            context.startActivities(intents);
        context.startActivity(mIntent == null ? mainIntent : mIntent);
//        } else {
//            //如果app进程已经被杀死，先重新启动app，将DetailActivity的启动参数传入Intent中，参数经过
//            //SplashActivity传入MainActivity，此时app的初始化已经完成，在MainActivity中就可以根据传入             //参数跳转到DetailActivity中去了
//            //app在后台
//
//
//            Intent launchIntent = new Intent(context, SplashActivity.class);
//            launchIntent.setFlags(
//                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//            Bundle args = new Bundle();
//            launchIntent.putExtra(Constants.EXTRA_BUNDLE, args);
//            context.startActivity(launchIntent);
//        }
    }
}